package com.malty.mall.domain.model;

import com.malty.mall.domain.entity.ums.UmsResource;
import com.malty.mall.domain.entity.ums.UserAdmin;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author malty
 */
public class AdminUserDetails implements UserDetails {
    private final String username;
    private final String password;

    private final Boolean enabled;
    private final List<UmsResource> resourceList;

    public AdminUserDetails(UserAdmin userAdmin, List<UmsResource> resourceList) {
        this.username = userAdmin.getUsername();
        this.password = userAdmin.getPassword();
        this.enabled = "1".equals(userAdmin.getStatus());
        this.resourceList = resourceList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的角色
        return resourceList.stream()
                .map(role -> new SimpleGrantedAuthority(role.getId() + ":" + role.getName()))
                .collect(Collectors.toList());
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }
}
